<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>报表统计 - 电动车销售系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/purchase.css">
    <style>
        body {
            background-color: #f8f9fa;
        }

        .report-card {
            background: #ffffff;
            border-radius: 12px;
            padding: 30px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
            margin-bottom: 30px;
        }

        .chart-container {
            height: 400px;
            background: #fff;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }

        /* 表格样式恢复为原来的浅灰色表头 */
        table.table th {
            background-color: #f1f1f1;
            color: #333;
            text-align: center;
            font-weight: 600;
        }

        table.table td {
            vertical-align: middle;
            text-align: center;
        }
    </style>
</head>
<body>
<header class="dashboard-header">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h1 class="h3 mb-0"><i class="bi bi-bicycle"></i> 电动车销售管理系统</h1>
                <p class="mb-0 opacity-75">报表统计中心</p>
            </div>
        </div>
    </div>
</header>

<div class="container my-4">
    <div class="report-card">
        <div>
            <h2 class="mb-0"><i class="bi bi-people me-2"></i>报表统计</h2>
            <p class="text-muted mb-0">统计订单信息</p>
        </div>
        <div>
            <a href="${pageContext.request.contextPath}/AdminDashboardServlet" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left me-1"></i> 返回管理
            </a>
        </div>

        <!-- 表单 -->
        <form action="${pageContext.request.contextPath}/ReportServlet" method="get" class="row g-3 mb-4">
            <div class="col-md-3">
                <select class="form-select" name="reportType" required>
                    <option value="purchase" ${reportType == 'purchase' ? 'selected' : ''}>进车订单</option>
                    <option value="sale" ${reportType == 'sale' ? 'selected' : ''}>销售订单</option>
                    <option value="after_sale" ${reportType == 'after_sale' ? 'selected' : ''}>售后订单</option>
                </select>
            </div>
            <div class="col-md-3">
                <select class="form-select" name="statBy" required>
                    <option value="user" ${statBy == 'user' ? 'selected' : ''}>按工作人员</option>
                    <option value="time" ${statBy == 'time' ? 'selected' : ''}>按时间统计</option>
                </select>
            </div>
            <div class="col-md-3">
                <input type="date" class="form-control" name="startDate" value="${startDate}" required>
            </div>
            <div class="col-md-3">
                <input type="date" class="form-control" name="endDate" value="${endDate}" required>
            </div>
            <div class="col-md-12">
                <button type="submit" class="btn btn-primary">生成报表</button>
            </div>
        </form>

        <!-- 数据展示 -->
        <c:if test="${not empty reportData}">
            <div class="table-responsive mb-4">
                <table class="table table-bordered table-hover align-middle">
                    <thead>
                    <tr>
                        <c:choose>
                            <c:when test="${statBy == 'user'}">
                                <th>员工ID</th>
                                <th>员工姓名</th>
                            </c:when>
                            <c:otherwise>
                                <th>日期</th>
                            </c:otherwise>
                        </c:choose>
                        <th>订单数量</th>
                        <th>总数量</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${reportData}" var="row" varStatus="status">
                        <tr>
                            <c:choose>
                                <c:when test="${statBy == 'user'}">
                                    <td>${row.id}</td>
                                    <td>${row.name}</td>
                                </c:when>
                                <c:otherwise>
                                    <td>${row.order_date}</td>
                                </c:otherwise>
                            </c:choose>
                            <td>${row.order_count}</td>

                            <!-- 只在第一行显示总数量，并合并单元格 -->
                            <c:if test="${status.first}">
                                <td rowspan="${dataSize}" style="vertical-align: middle; text-align: center;">
                                        ${totalCount}
                                </td>
                            </c:if>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>

            <!-- 饼状图 -->
            <div class="chart-container">
                <canvas id="reportPieChart"></canvas>
            </div>

            <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
            <script>
                const ctx = document.getElementById('reportPieChart').getContext('2d');
                const pieChart = new Chart(ctx, {
                    type: 'pie',
                    data: {
                        labels: [
                            <c:forEach items="${reportData}" var="row" varStatus="status">
                            <c:choose>
                            <c:when test="${statBy == 'user'}">
                            '${row.name}'
                            </c:when>
                            <c:otherwise>
                            '${row.order_date}'
                            </c:otherwise>
                            </c:choose>
                            <c:if test="${!status.last}">,</c:if>
                            </c:forEach>
                        ],
                        datasets: [{
                            label: '订单数量',
                            data: [
                                <c:forEach items="${reportData}" var="row" varStatus="status">
                                ${row.order_count}
                                <c:if test="${!status.last}">,</c:if>
                                </c:forEach>
                            ],
                            backgroundColor: [
                                '#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF',
                                '#FF9F40', '#8AC926', '#1982C4', '#6A4C93', '#FF595E'
                            ],
                            hoverOffset: 4
                        }]
                    },
                    options: {
                        responsive: true,
                        maintainAspectRatio: false,
                        plugins: {
                            legend: {
                                position: 'right',
                                labels: {
                                    font: {
                                        size: 14
                                    }
                                }
                            },
                            tooltip: {
                                callbacks: {
                                    label: function(context) {
                                        const label = context.label || '';
                                        const value = context.raw;
                                        const total = context.dataset.data.reduce((a, b) => a + b, 0);
                                        const percentage = Math.round((value / total) * 100);
                                        return `${label}: ${value} (${percentage}%)`;
                                    }
                                }
                            }
                        }
                    }
                });
            </script>
        </c:if>

        <c:if test="${empty reportData}">
            <div class="text-center py-5">
                <i class="bi bi-clipboard-data display-4 text-muted mb-3"></i>
                <h4>暂无报表数据</h4>
                <p class="text-muted">请选择报表类型和时间范围后生成报表</p>
            </div>
        </c:if>
    </div>
</div>
</body>
</html>